<?php

namespace app\admin\model;

use think\Collection;
use PDOStatement;
use think\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\exception\DataNotFoundException;
use think\Model;
use traits\model\SoftDelete;

class Question extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'question';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'kind_text',
        'difficulty_text',
        'status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }


    const kindList       = ['JUDGE', 'SINGLE', 'MULTI', 'FILL', 'SHORT', 'MATERIAL'];
    const difficultyList = ['EASY', 'GENERAL', 'HARD'];
    const statusList     = ['NORMAL', 'HIDDEN'];

    public function getKindList()
    {
        return [
            'SINGLE'   => '单选题',
            // 'MULTI'    => '多选题',
            // 'JUDGE'    => '判断题',
            // 'FILL'     => '填空题',
            'SHORT'    => '简答题',
            // 'MATERIAL' => '材料题',
        ];
    }

    public function getDifficultyList()
    {
        return ['EASY' => '简单', 'GENERAL' => '普通', 'HARD' => '困难'];
    }

    public function getStatusList()
    {
        return ['NORMAL' => '正常', 'HIDDEN' => '隐藏'];
    }


    public function getKindTextAttr($value, $data)
    {
        $value = $value ?: ($data['kind'] ?? '');
        $list = $this->getKindList();
        return $list[$value] ?? '';
    }


    public function getDifficultyTextAttr($value, $data)
    {
        $value = $value ?: ($data['difficulty'] ?? '');
        $list = $this->getDifficultyList();
        return $list[$value] ?? '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }



    public function mock()
    {
        return $this->belongsTo(Mock::class, 'mock_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }



    /**
     * 固定选题回显 刷题模式
     * @param mixed $question_group_id 
     * @param array $with 
     */
    public static function getFixListByPaper($question_group_id, $with = [])
    {
        $questions = self::with($with)
            ->alias('question')
            ->join('question_paper pq', 'question.id = pq.question_id')
            ->where('pq.question_group_id', $question_group_id)
            ->field('question.*, pq.answer_config')
            ->order('pq.id', 'asc')
            ->select();
        foreach ($questions as &$question) {
            if (!empty($question['answer_config'])) {
                // 简答题 - 替换答案分数配置
                if ($question['kind'] == 'SHORT') {
                    $question['answer'] = $question['answer_config'];
                }
            }
        }
        return $questions;
    }


    /**
     * 固定选题 考试模式
     * @param mixed $paper_id 
     * @param array $with 
     * @return Collection|false|PDOStatement|string 
     * @throws DbException 
     * @throws ModelNotFoundException 
     * @throws DataNotFoundException 
     */
    public static function getExamFixListByPaperId($paper_id, $with = [])
    {
        $questions = self::with($with)
            ->alias('question')
            ->join('exam_paper_question pq', 'question.id = pq.question_id')
            ->where('pq.paper_id', $paper_id)
            ->field('question.*, pq.answer_config,pq.score')
            ->order('pq.id', 'asc')
            ->select();
        foreach ($questions as &$question) {
            if (!empty($question['answer_config'])) {
                // 简答题 - 替换答案分数配置
                if ($question['kind'] == 'SHORT') {
                    $question['answer'] = $question['answer_config'];
                }
            }
        }
        return $questions;
    }
}
